package is202.hrms.web;

import is202.hrms.ejb.UserEJB;
import is202.hrms.entity.Employee;
import is202.hrms.entity.Usere;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 * The backing bean for userartment.xhtml. The Named annotation marks it
 * as a CDI backing bean. The ConversationScoped annotation controls
 * the lifecycle of the bean. The lifetime of a ConversationScoped bean is
 * explicitly managed in the code.
 *
 * @author even
 */
@Named("userbean")
@ConversationScoped
public class UserBean implements Serializable {

    // The @Inject annotation is a request to the server to
    // give us a Conversation object, and store it in conv
    @Inject private Conversation conv;
    @EJB UserEJB userEjb;
    private Usere user;
    private boolean updating;

    // Usere properties - These properties will be copied to a Usere
    // object, when the time comes to save the data in the database.
    private int userId;
    private String name;
    private String email;
    private int rank;
    private List<Employee> employees;

    public UserBean() {
    }

    /**
     * called when the user selects a userartment from the list in
     * userartments.xhtml.
     */
    public void setUserId(int userId) {
        if (conv.isTransient()) {
            conv.begin();
        }

        user = userEjb.find(userId);
        System.out.println(user.getName());
        if (null == user) {
            // we will get here if userId is not a valid, or if
            // userId is valid but there is no userartment with that userno
            updating = false;
            userId = 0;
           // name = "";
         //   employees = new ArrayList<Employee>();
        }
        else {
            updating = true;
            this.userId = user.getId();
            name = user.getName();
          //  employees = user.getEmployees();
        }
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }
    
    public int getUserId() {
        return userId;
    }

    public String getName() {
        return name;
    }
    
    public String getEmail(){
        return email;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public boolean isUpdating() {
        return updating;
    }
    
    public void setEmail(String email){
        this.email = email;
    }

    private Usere createUserObject() {
        // Create a userartment object containing the data
        // from the form. The object can be passed on to
        // userListBean
        user = new Usere();
        user.setId(userId);
        user.setName(name);
        user.setEmail(email);
        user.setRank(1);
        return user;
    }

    public View saveUser() {
         user = createUserObject();
        if (user.getId() > 0) userEjb.update(user);
        else userEjb.insert(user);
        conv.end();
        return View.department;
    }

    public View deleteUser() {
        user = createUserObject();
        userEjb.delete(user);
        conv.end();

        return View.department;
    }
}
